<?php

require('./configs/include.php');

class c_registrar_categoria extends super_controller {

    public function registrar() {

        
        
        if (is_uploaded_file($this->files->imagen['tmp_name'])) {
            $img_type = $this->files->imagen['type'];
            $img_name = date("D jS M Y h i s a e");
            $subcadena = explode(".", $this->files->imagen['name']);
            $type = end($subcadena);
            if (((strpos($img_type, "gif") || strpos($img_type, "jpeg") ||
                    strpos($img_type, "jpg")) || strpos($img_type, "png"))) {
                if (!@move_uploaded_file($this->files->imagen['tmp_name'], C_FULL_PATH . "recursos/" . $img_name . "." . $type)) {
                    throw_exception("No se guardo la imagen");
                }
                $producto = new producto();
                $producto->set('foto', $this->gvar['l_global'] . "recursos/" . $img_name . "." . $type);
                $producto->set('nombre', $this->post->nombre);
                $producto->set('descripcion', $this->post->descripcion);
                $producto->set('precio', $this->post->precio);
                $producto->set('categoria', $this->post->categoria);

                $this->orm->connect();
                $this->orm->insert_data("normal", $producto);
                $this->orm->close();
            } else {
                throw_exception("Formato de imagen desconocido");
            }
        } else {
            throw_exception("No se pudo cargar la imagen");
        }

        $this->type_warning = "success";
        $this->msg_warning = "El producto se registro correctamente";
        $this->temp_aux = 'message.tpl';
        $this->engine->assign('type_warning', $this->type_warning);
        $this->engine->assign('msg_warning', $this->msg_warning);
    }

    public function display() {

        $options['categoria']['lvl2'] = "all";

        $this->orm->connect();
        $this->orm->read_data(array("categoria"), $options);
        $categoria = $this->orm->get_objects("categoria");
        $this->orm->close();

        $where = array(array('name' => $this->gvar['n_registrar_producto'], 'link' => $this->gvar['l_registrar_producto']));
        $this->engine->assign('categoria', $categoria);
        $this->engine->assign('title', $this->gvar['n_registrar_producto']);
        $this->engine->assign('active', $this->gvar['n_registrar_producto']);
        $this->engine->assign('where', $where);
        $this->engine->display('header2.tpl');
        $this->engine->display($this->temp_aux);
        $this->engine->display('registrar_producto.tpl');
        $this->engine->display('footer2.tpl');
    }

    public function run() {
        try {
            if (isset($this->get->option)) {
                $this->{$this->get->option}();
            }
        } catch (Exception $e) {
            $this->error = 1;
            $this->msg_warning = $e->getMessage();
            $this->temp_aux = 'message.tpl';
            $this->engine->assign('type_warning', $this->type_warning);
            $this->engine->assign('msg_warning', $this->msg_warning);
        }

        $this->display();
    }

}

$call = new c_registrar_categoria();
$call->run();
?>